package com.example.statistics.ex

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.DisplayMetrics
import android.view.LayoutInflater
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.example.statistics.bean.DocumentBean
import com.example.statistics.common.FileSelectorActivity
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityForResult
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jhj.imageselector.ImageSelector
import com.jhj.imageselector.bean.LocalMedia
import org.jetbrains.anko.windowManager
import java.io.BufferedReader
import java.io.InputStreamReader


val Context.screenHeight: Int
    get() {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val metric = DisplayMetrics()
            windowManager.defaultDisplay.getRealMetrics(metric)
            metric.heightPixels
        } else {
            resources.displayMetrics.heightPixels
        }
    }

val Context.screenWidth: Int
    get() {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            val metric = DisplayMetrics()
            windowManager.defaultDisplay.getRealMetrics(metric)
            metric.widthPixels
        } else {
            resources.displayMetrics.widthPixels
        }
    }

val Context.inflater: LayoutInflater
    get() = LayoutInflater.from(this)

fun Context.getResColor(@ColorRes color: Int): Int {
    return ContextCompat.getColor(this, color)
}

fun Context.getResDrawable(@DrawableRes id: Int): Drawable {
    return ContextCompat.getDrawable(this, id) ?: resources.getDrawable(id)
}

fun Context.readAssets(name: String): String {
    val inputStream = resources.assets.open(name)
    val text = inputStream.use {
        val buf = BufferedReader(InputStreamReader(inputStream, "utf-8"))
        return@use buf.readText()
    }
    return text
}

inline fun <reified T> Context.assets(assetsFileName: String): T {
    val inputStream = resources.assets.open(assetsFileName)
    val text = inputStream.use {
        val buf = BufferedReader(InputStreamReader(inputStream, "utf-8"))
        return@use buf.readText()
    }
    return Gson().fromJson(text, object : TypeToken<T>() {}.type)
}

fun Activity.imageDisplay(list: List<String>, index: Int = 0) {
    val localMediaList = list.map { LocalMedia(it) }
    ImageSelector.preview(this, localMediaList, index)
}

fun Activity.imageSelected(list: List<String> = listOf(), maxNum: Int = 100, body: (List<LocalMedia>) -> Unit) {
    val localMediaList = list.map { LocalMedia(it) }
    ImageSelector.multiSelected(this, localMediaList, maxNum, body = body)
}

fun FragmentActivity.fileSelected(body: (DocumentBean) -> Unit) {
    activityForResult<FileSelectorActivity>() {
        val bean = it?.getParcelableExtra<DocumentBean>(Constants.DATA)
        if (bean != null) {
            body(bean)
        }
    }
}


